#include "StdAfx.h"
#include "Texture.h"

namespace {
	GLuint makeTex(glm::ivec2 & dim, GLint internalFormat, GLenum inputFormat, GLenum inputType, GLint filter, GLint wrap, const void* data) {
		assert(dim.x > 0 && dim.y > 0);
		GLuint id = -1;
		glGenTextures(1, &id);
		glBindTexture(GL_TEXTURE_2D, id);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, filter);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, filter);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap);	
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap);	
		glTexImage2D(GL_TEXTURE_2D, 0, internalFormat, dim.x, dim.y, 0, inputFormat, inputType, data);
		::flushGLError("::Texture()");
		return id;
	}
}

Texture::Texture()
{
    created = false;
}

Texture::~Texture(){

};

Texture::Texture(glm::ivec2 & dim, GLint internalFormat, GLenum inputFormat, GLenum inputType, GLint filter, GLint wrap, const void* data) 
    : dim(dim)
{
	texId = makeTex(dim, internalFormat, inputFormat, inputType, filter, wrap, data);

	created = true;
}

void Texture::bind() const
{
    glBindTexture(GL_TEXTURE_2D, texId);
}

void Texture::unbind() const
{
    glBindTexture(GL_TEXTURE_2D, 0);
}

// ----------------------

PingPongTexture::PingPongTexture() {
	created = false;
}

PingPongTexture::PingPongTexture(glm::ivec2 & dim, GLint internalFormat, GLenum inputFormat, GLenum inputType, GLint filter, GLint wrap, const void* data) : 
	current_id(0), dim(dim)
{
	tex_id[0] = makeTex(dim, internalFormat, inputFormat, inputType, filter, wrap, data);
	tex_id[1] = makeTex(dim, internalFormat, inputFormat, inputType, filter, wrap, data);

	created = true;
}

//void PingPongTexture::bind() const {
//	glBindTexture(GL_TEXTURE_2D, tex_id[current_id]);
//}
//
//void PingPongTexture::unbind() const {
//	 glBindTexture(GL_TEXTURE_2D, 0);
//}
